package com.book.controller;


import com.book.po.student;
import com.book.dao.studentDao;
import com.book.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import com.book.service.noticeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class studentController {
    @Autowired
    private studentService studentService;
    @Autowired
    private noticeService noticeService;
    @Autowired
    private studentDao studentDao;
    @RequestMapping("/login")
    public String login(Model model, HttpSession session, student student){
        student student1 = studentService.login(student);

        if (student1!=null){
            session.setAttribute("student",student1);
            model.addAttribute("msg","");
            int count = noticeService.notice_count(student.getId());
            model.addAttribute("count",count);
            return "user.html";
        }else {
            model.addAttribute("msg","账号或密码错误");
            return "userLogin.html";
        }

    }

    @RequestMapping("/quit")
    public String quit(RedirectAttributes  model,HttpSession session){
        session.removeAttribute("student");
//        session.removeAttribute("bookTypes");
//        session.removeAttribute("readerTypes");
        return "userLogin.html";
    }
    @RequestMapping("/student_select")
    public String admin_select(RedirectAttributes model, student student){
        List<student> students = studentService.selectDetail(student);
        for(student s:students){
            s.setCount(studentDao.selectOdCount(s.getId()));
        }
        if (students.size()>=1){
            model.addFlashAttribute("students",students);
            model.addFlashAttribute("msg","");
            return "redirect:/manage/bookManage";
        }else {
            model.addFlashAttribute("msg","没有查找用户！");
            return "redirect:/manage/bookManage";
        }
    }
//
//
//    @RequestMapping("/admin_insert")
//    public String admin_insert(RedirectAttributes  model, Admin admin){
//        String msg = adminservice.admin_insert(admin);
//        model.addFlashAttribute("msg",msg);
//        return "redirect:/manage/adminManage";
//    }
//
//    @RequestMapping("/admin_update")
//    public String admin_update(RedirectAttributes  model, Admin admin){
//        String msg = adminservice.admin_update(admin);
//        model.addFlashAttribute("msg",msg);
//        return "redirect:/manage/adminManage";
//    }
//
//    @RequestMapping("/admin_delete")
//    public String book_delete(RedirectAttributes  model, Integer aid){
//        String msg = adminservice.admin_delete(aid);
//        model.addFlashAttribute("msg",msg);
//        return "redirect:/manage/adminManage";
//    }
}
